package com.yx.cloud.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.yx.cloud.R;
import com.yx.cloud.utils.RomUtils;
import com.iot.product.sweeper.bean.Robot;
import com.iot.common.utils.LogUtils;

public class LanguageAdapter extends RecyclerView.Adapter implements View.OnClickListener {
    private String[] mList;
    private Context mContext;
    private View view;
    private boolean isClick;
    private int currentPosition;

    private OnItemClickListener mOnItemClickListener = null;

    public void setCurrentPosition(int currentPosition, boolean isClick) {
        this.currentPosition = currentPosition;
        this.isClick = isClick;
    }

    public int getCurrentPosition() {
        return currentPosition;
    }

    public boolean isClick() {
        return isClick;
    }

    public LanguageAdapter(Context context, String[] list) {
        mList = list;
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(mContext).inflate(R.layout.language_adapter_item, parent, false);
        LanguageViewHolder lv = new LanguageViewHolder(view);
        view.setOnClickListener(this);
        return lv;
    }
//    private void ItemClickEvent(LanguageViewHolder holder) {
//        LogUtils.i("LanguageAdapter ItemClickEvent ----");
//        holder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                LogUtils.i("LanguageAdapter onClick ----");
//                if (!isClick){
//                    setCurrentPosition(holder.getBindingAdapterPosition(), true);
//                }else {
//                    setCurrentPosition(holder.getBindingAdapterPosition(), getCurrentPosition() != holder.getBindingAdapterPosition());
//                }
//                notifyDataSetChanged();
//            }
//        });
//    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        LogUtils.i("LanguageAdapter onBindViewHolder ----");
        String ss = mList[position];
        ((LanguageViewHolder) holder).mLanguageName.setText(ss);
        if (getCurrentPosition() == position && isClick()) {
            ((LanguageViewHolder) holder).mLanguageIcon.setVisibility(View.VISIBLE);
        } else {
            ((LanguageViewHolder) holder).mLanguageIcon.setVisibility(View.GONE);
        }
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return mList.length;
    }


    class LanguageViewHolder extends RecyclerView.ViewHolder {
        public TextView mLanguageName;
        public ImageView mLanguageIcon;

        public LanguageViewHolder(View itemView) {
            super(itemView);
            mLanguageName = itemView.findViewById(R.id.tv_languageName);
            mLanguageIcon = itemView.findViewById(R.id.iv_languageIcon);
        }
    }

    @Override
    public void onClick(View v) {
        LogUtils.i("LanguageAdapter onClick ----");
        if (RomUtils.isFastClick()) {
            return;
        }
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(v, (Integer) v.getTag());
        }
    }
    /**
     * Item点击事件方法
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        LogUtils.i("LanguageAdapter setOnItemClickListener ----");
        this.mOnItemClickListener = listener;
    }
    //自定义一个回调接口来实现Click和LongClick事件
    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }
}
